import { ChannelConfig } from "@/config/ChannelConfig";
import { DateUtil } from "@/utils/DateUtil";
import { LocalStorage } from "@/utils/LocalStorage";
import { Logger } from "@/utils/Logger";
import { RandomUtil } from "@/utils/RandomUtil";

const UserLocalKey: string = "UserLocalKey"

export class UserModel {

    public static instance: UserModel = new UserModel();

    public static UserStatus = {
        Normal: 0,
        Disable: 1,
    }

    public id: number;
    public userName: string = ""
    public pass: string = ""
    public roleId: number;
    public channelList: Array<number> = [];
    public remarks: string = "" //备注
    public status: number;
    public lastLoginTime:number;

    public static modelList: Array<UserModel> = [];

    public static totalNum: number = 50;

    public static initTest() {
        this.modelList = [];
        let channelList: Array<ChannelConfig> = ChannelConfig.getList(false)
        for (let i = 0; i < this.totalNum; i++) {
            let model: UserModel = new UserModel();
            model.id = i;
            model.userName = "user" + i;
            model.pass = "pass"+i
            model.roleId = RandomUtil.nextInt(1, 9)
            let randomtemp: Array<ChannelConfig> = RandomUtil.randomArr(channelList, RandomUtil.nextInt(1, channelList.length));
            for (let j = 0; j < randomtemp.length; j++) {
                model.channelList[j] = randomtemp[j].id;
            }
            model.remarks = "remarkd" + i
            model.status = RandomUtil.nextInt(0, 1)
            this.modelList[i] = model;
        }
        // Logger.log("UserModel.initTest", channelList, this.modelList)
    }

    public init() {
        let str: string = LocalStorage.getItem(UserLocalKey);
        try {
            let obj: object = JSON.parse(str);
            UserModel.instance.userName = obj["userName"];
            UserModel.instance.pass = obj["userName"];
            UserModel.instance.lastLoginTime = obj["lastLoginTime"]
        } catch (error) {

        }
        Logger.log("UserModel.init===", str);
    }

    public static needShowLogin(){
        // if(UserModel.instance.userName && (DateUtil.now()-this.lastLoginTime <= 1000*60*60)){
        if(UserModel.instance.userName && (DateUtil.now()-UserModel.instance.lastLoginTime <= 1000*60)){
            return false
        }
        return true;
    }

    public login(userName: string, pass: string) {
        this.userName = userName;
        this.pass = pass;
        this.lastLoginTime = DateUtil.now();
        this.saveToLocal();
    }

    public logout() {
        this.userName = "";
        this.pass = "";
        this.lastLoginTime = 0
        this.saveToLocal()
    }

    private saveToLocal() {
        let str: string = JSON.stringify(UserModel.instance);
        Logger.log("UserModel.saveToLocal=", str)
        LocalStorage.setItem(UserLocalKey, str);
    }

    public static getShowList(listQuery, callback: Function) {
        let showList: Array<UserModel> = [];
        // HttpClient.queryServerList(listQuery["page"], listQuery["limit"], () => {
        // showList = this.modelList
        let startIndex: number = (listQuery["page"] - 1) * listQuery["limit"]
        let endEndex: number = startIndex + listQuery["limit"]
        for (let i = startIndex; i < endEndex; i++) {
            let model: UserModel = this.modelList[i];
            if (model) {
                showList.push(model)
            }
        }
        Logger.log("UserModel.getShowList", showList)
        callback && callback(showList);
        // })

    }

    public static setDisable(id:number){
        for(let i=0; i<this.modelList.length; i++){
            if(this.modelList[i].id == id){
                this.modelList[i].status = UserModel.UserStatus.Disable
                break;
            }
        }
    }

}